<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">
		<template v-if="isLogin">
			<view class="body">
				<view class="map">
					<map id="map" style="width: 100%; height: 400rpx;"  
						scale="16" 
						:latitude="params.latitude" 
						:longitude="params.longitude" 
						:markers="markers" 
						:circles="circles"
						:polyline="polylines" > 
					</map>
					
				</view>
				
				<view class="form-wrap m-b2">

					<view class="form-item">
						<view class="label">经纬度：</view>
						<view class="form-input">
							<view class="content">
								<input type="text" class="inp" :value="params.localtion" placeholder="" disabled /> 
								
							</view>
						</view>
					</view>
<!-- 
					<view class="form-item">
						<view class="label">定位精度：</view>
						<view class="form-input">
							<view class="content">
								<view class="inp">{{params.accuracy}} （<text
										:class="'leavel_'+params.leavel_num">{{params.leavel}}</text>）</view>
							</view>
						</view>
					</view> -->

					<view class="form-item">
						<view class="label">活蚁巢数量：</view>
						<view class="form-input">
							<view class="content">
								<input type="text" class="inp" v-model="params.nestNum" placeholder="活蚁巢数量" disabled />
								<view class="link">个</view>
							</view>
						</view>
					</view>

				</view>

				<view class="btn" @click="submit">定位</view>

			</view>

			<!-- 底部 -->
			<view class="footer">
				<view class="fix-wrap bottom">
					<navigator open-type="navigateBack"  hover-class="none" class="form-lay-btn" >定位完成，返回继续作业</navigator>
				</view>
			</view>


		</template>
	</view>
</template>

<script>
	import {
		gd_map_Key,
		tc_map_key
	} from '@/config.js'
	import {
		getCurrenPage,
		d2Dms
	} from '@/common/util.js'
	export default {
		name: 'name',
		data() {
			return {
				isLogin: true,
				isIphoneX: getApp().globalData.isIphoneX,
				userInfo: uni.getStorageSync('userInfo'),
				isStop: true,
				params: {
					type:'',
					fid: '',
					pid: '',
					accuracy: '',
					nestNum: 0,
					datatype: '0',
					address: '',
					leavel: '',
					leavel_num: 0,
					latitude: '',
					longitude: '',
					localtion: '',
					prevlatitude:'',					
					prevlongitude: '',
					speed: '',
					times1: 1
				},
				timer:null,
				market_id:1,
				markers: [],
				circles: [],
				polylines: [{
					points: [],
					color: "#fcd535",
					width: 10
				}],
				loading:false
			}


		},
		async onLoad(params) { 
			console.log(params)
			this.isLogin = await this.$loginState('check');
			this.params.localtion = d2Dms(parseFloat(params.lng)) + '，' + d2Dms(parseFloat(params.lat));
			
			this.params.leavel = params.level;
			this.params.leavel_num = params.leavel_num; 
			this.params.fid = params.fid;
			this.params.pid = params.pid;
			this.params.type = params.type;
			
			if(uni.getStorageSync('markers_'+params.pid).length>0){
				this.markers=uni.getStorageSync('markers_'+params.pid);
				this.params.nestNum = this.markers.length-1;
			}else{
				this.params.nestNum = parseInt(params.nestNum);
			}
			
			this.getLocal();
			
		},
		async onShow() {

		},
		onUnload() {
			clearTimeout(this.timer)
		},
		methods: {
			
			// 获取当前位置
			getLocal: function() {
				let _this = this;
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: function(res) { 
			
						_this.params.accuracy = res.accuracy;
						_this.params.latitude = res.latitude;
						_this.params.longitude = res.longitude;
						_this.params.speed = res.speed;
						_this.params.altitude = res.altitude;
						_this.params.localtion = d2Dms(parseFloat(res.longitude)) + '，' + d2Dms(parseFloat(res.latitude));
						
						_this.circles[0]={
							latitude: _this.params.latitude,
							longitude: _this.params.longitude,
							color: "#ff00000A",
							fillColor: "#ff00003A",
							radius: "100"
						}
						
						_this.markers[0]={
							id: 0,
							latitude: parseFloat(_this.params.latitude),
							longitude: parseFloat(_this.params.longitude),
							iconPath: '/static/images/my_local.png', 
						};		  
					}
				});
			},

			//蚁巢定位上传
			updateGPS1: function() { 
				let _this = this;  
				if(_this.loading){
					uni.showToast({
						icon: "none",
						title: '两次定位间隔时间太短',
						mask: true,
						duration: 1000, 
					})
				}
				
				if (this.isStop) {
					this.isStop = false;									
					
					uni.showLoading({
						title:'定位中...'
					})	 
					uni.getLocation({
						type: 'gcj02',
						isHighAccuracy: true,
						altitude:true,
						highAccuracyExpireTime:5000,
						accuracy:50,
						async success(res) {    
							console.log('位置',res)
							if(res.accuracy<_this.params.accuracy){ 
								_this.params.speed = res.speed;
								_this.params.accuracy = res.accuracy;
								_this.params.latitude = res.latitude;
								_this.params.longitude = res.longitude;
							}
							
							if(res.accuracy>30 && _this.params.times1<2){  
								++_this.params.times1;
								_this.timer=setTimeout(() => {
									_this.isStop = true;
									_this.updateGPS();
								}, 1000) 
							}else{   
								_this.params.times1=1;
								let local_res={};
								let params={
									'type': 1,
									'fid': _this.params.fid,
									'p_id': _this.params.pid,
									'speed': _this.params.speed,
									'accuracy': _this.params.accuracy,
									'gcj02_lat': res.latitude,
									'gcj02_lng': res.longitude,
									'create_uid': _this.userInfo.id,
									'timesid': _this.params.type == 'jl' ? _this.params.timesid : 0
								} 
								if(_this.params.type=='jc'){
									local_res = await _this.$api.pro_api.pcnestUpdateGps(params); 
								}
								
								if (_this.params.type == 'jl') {
									local_res = await _this.$api.pro_api.jlnestUpdateGps(params); 
								}
								if (_this.params.type == 'publicpc') {
									local_res = await _this.$api.pro_api.commonnestUpdateGps(params); 
								} 
																			
								  
								if(local_res.data.ret==0){
									uni.hideLoading();								
									_this.params.nestNum += 1;
									_this.params.accuracy = res.accuracy;
									_this.params.latitude = res.latitude;
									_this.params.longitude = res.longitude;
									_this.params.localtion = d2Dms(parseFloat(res.longitude)) + '，' + d2Dms(parseFloat(res.latitude));
									if (res.accuracy <= 10) {
										_this.params.leavel = "优";
										_this.params.leavel_num = 1;
									} else if (res.accuracy > 10 && res.accuracy <= 50) {
										_this.params.leavel = "良";
										_this.params.leavel_num = 2;
									} else {
										_this.params.leavel = "差";
										_this.params.leavel_num = 3;
									}
									
									let marker={
										id:_this.params.nestNum,
										latitude:parseFloat(res.latitude),
										longitude:parseFloat(res.longitude),
										iconPath:'../../../static/images/mark9.png'
									}
									
									_this.markers=_this.markers.concat(marker)
									 
									
									uni.setStorage({
										key:'markers_'+_this.params.pid,
										data:_this.markers
									})
									
									uni.showToast({
										icon: "success",
										title: "定位成功",
										mask: true,
										duration: 1000,
										success() {
											
											_this.loading=true;
											setTimeout(() => {
												_this.isStop = true;
												_this.loading=false;
											}, 10000)
										}
									})
									
									uni.$emit("updateNestNum",{nestNum: _this.params.nestNum});     // 蚁巢数量更新 更新上页蚁巢数 jcForm 页面 
								}else{
									uni.hideLoading();
									uni.showToast({
										icon: "success",
										title: res.data.message,
										mask: true,
										duration: 1000,
										success() {
											setTimeout(() => {
												_this.isStop = true;
											}, 10000)
										}
									})
								}		 
							}			
						},
						fail() {
							uni.hideLoading(); 
							uni.showToast({
								icon: "success",
								title: "定位失败",
								mask: true,
								duration: 1000,
								success() {
									setTimeout(() => {
										_this.isStop = true;
									}, 1500)
								}
							})
						}
					})
				}
			
			
			},
			
			//蚁巢定位上传
			submit: async function() { 
				let _this = this;  
				
				if(this.loading){
					uni.showToast({
						icon: "none",
						title: '两次定位间隔时间太短',
						mask: true,
						duration: 1000, 
					})
					return
				}
				
				uni.showLoading({
					title:'定位中...',
					mask:true
				})	 
				
				if (this.isStop) {
					this.isStop = false;						
					this.updateGPS(); 					 
				}
			
			
			},
			
			updateGPS: function() {
				let _this = this;  
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude:true,
					highAccuracyExpireTime:5000,
					accuracy:50,
					async success(res) {     
						_this.circles[0]={
							latitude: _this.params.latitude,
							longitude: _this.params.longitude,
							color: "#ff00000A",
							fillColor: "#ff00003A",
							radius: "100"
						}
						 
						
						if(res.accuracy<_this.params.accuracy){ 
							_this.params.speed = res.speed;
							_this.params.accuracy = res.accuracy;
							_this.params.latitude = res.latitude;
							_this.params.longitude = res.longitude;
						}
						
						if(res.accuracy>30 && _this.params.times1<2){  
							++_this.params.times1;
							_this.timer=setTimeout(() => {
								_this.isStop = true;
								_this.updateGPS();
							}, 1000) 
						}else{   
							_this.params.times1=1;
							let local_res={};
							let params={
								'type': 1,
								'fid': _this.params.fid,
								'p_id': _this.params.pid,
								'speed': _this.params.speed,
								'accuracy': _this.params.accuracy,
								'gcj02_lat': res.latitude,
								'gcj02_lng': res.longitude,
								'create_uid': _this.userInfo.id,
								'timesid': _this.params.type == 'jl' ? _this.params.timesid : 0
							} 
							if(_this.params.type=='jc'){
								local_res = await _this.$api.pro_api.pcnestUpdateGps(params); 
							}
							
							if (_this.params.type == 'jl') {
								local_res = await _this.$api.pro_api.jlnestUpdateGps(params); 
							}
							if (_this.params.type == 'publicpc') {
								local_res = await _this.$api.pro_api.commonnestUpdateGps(params); 
							} 
																		
							  
							if(local_res.data.ret==0){
								uni.hideLoading();								
								_this.params.nestNum += 1;
								_this.params.accuracy = res.accuracy;
								_this.params.latitude = res.latitude;
								_this.params.longitude = res.longitude;
								_this.params.localtion = d2Dms(parseFloat(res.longitude)) + '，' + d2Dms(parseFloat(res.latitude));
								 
								 
								
								_this.marker_id += 1;
								
								let marker = {
									id: _this.marker_id,
									latitude: parseFloat(_this.params.latitude),
									longitude: parseFloat(_this.params.longitude),
									iconPath: '/static/images/mark9.png'
								}
								
								_this.markers = _this.markers.concat(marker);
								
								
								let local = {
									latitude: parseFloat(_this.params.latitude),
									longitude: parseFloat(_this.params.longitude),
								}
								_this.polylines[0].points.push(local)
								 
								
								uni.setStorage({
									key:'markers_'+_this.params.pid,
									data:_this.markers
								})
								
								uni.showToast({
									icon: "success",
									title: "定位成功",
									mask: true,
									duration: 1000,
									success() {
										 
										_this.loading=true; 
										setTimeout(() => {
											_this.loading=false; 
											_this.isStop = true; 
										}, 10000)
									}
								})
								
								uni.$emit("updateNestNum",{nestNum: _this.markers.length-1});     // 蚁巢数量更新 更新上页蚁巢数 jcForm 页面 
							}else{
								uni.hideLoading();
								uni.showToast({
									icon: "success",
									title: res.data.message,
									mask: true,
									duration: 1000,
									success() {
										setTimeout(() => {
											_this.isStop = true;
										}, 10000)
									}
								})
							}		 
						}			
					},
					fail() {
						uni.hideLoading(); 
						uni.showToast({
							icon: "success",
							title: "定位失败",
							mask: true,
							duration: 1000,
							success() {
								setTimeout(() => {
									_this.isStop = true;
								}, 1500)
							}
						})
					}
				})
			
			},
			
			 

		},
		components: {},

		computed: {

		},
		watch: {

		}

	}
</script>

<style lang="less" scoped>
	.map{
		position: relative;
		height: 400rpx;
		background: #ccc;
		cover-image{
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%,-50%);
			width: 50rpx;
			height: 50rpx;
		}
	}
	
	.form-wrap .label {
		width: 28%;
	}

	.form-wrap .content .link {
		color: #000;
	}
	.get-local{
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		position: absolute;
		right:0;
		top:0;
		image{
			width: 40rpx;
		}
		text{
			display: block;
			text-align: center;
			color:#1296db;
			font-size: 24rpx;
		}
		
	}
	.local-btn {
		position: absolute;
		right: 50rpx;
		top: 5rpx;
		padding: 3rpx 8rpx;
		background: #f00;
		border-radius: 5rpx;
		color: #fff;
		font-size: 30rpx;
	}

	.status {
		padding: 0 20rpx;
		color: #f00;
		font-size: 28rpx;
	}

	.hd-title {
		padding: 20rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.leavel_1 {
		color: #09f
	}

	.leavel_2 {
		color: #ff9600;
	}

	.leavel_3 {
		color: #f00;
	}

	.btn {
		border-radius: 50%;
		width: 250rpx;
		height: 250rpx;
		line-height: 250rpx;
		margin: 70rpx auto;
		background-image: linear-gradient(to right, #fe5d55, #e2231a);
		box-shadow: 0 0 10rpx #000;
		font-size: 40rpx;
		text-align: center;
		color: #fff;
	}

	.prev-btn {
		text-align: right;
		padding: 20rpx;

		text {
			padding: 4rpx 15rpx;
			background: #ff9600;
			border-radius: 8rpx;
			color: #fff;
			font-size: 32rpx;
		}
	}

	.prev-data {
		padding: 18rpx 10rpx;
		background: #f5f5f5;
		text-align: right;
		color: #999;
		font-size: 28rpx;

		.val {
			color: #333;
		}

		.use-btn {
			color: #d03c30;
		}
	}
</style>
